package h0723;

import java.util.*;

public class Anti_AddictionSystemForMobilePhones {
    static class App{
        String name;
        int priority; //优先级
        int startTime;
        int endTime;
        App(String name,int priority,int startTime,int endTime){
            this.name=name;
            this.priority=priority;
            this.startTime=startTime;
            this.endTime=endTime;
        }
    }
    //转换器
    public static int convert(String time){
        String[] tmp = time.split(":");
        int res=Integer.parseInt(tmp[0])*60+Integer.parseInt(tmp[1]);
        return res;
    }
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        // 需要注册的App
        ArrayList<App> apps = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            apps.add(new App(sc.next(), sc.nextInt(), convert(sc.next()), convert(sc.next())));
        }
        int queryTime = convert(sc.next());
        getResult(apps,queryTime);
    }

    //一个时间段只能使用一个App
    //App有优先级，数值越高，优先级越高。注册使用时段时，如果高优先级的App时间和低优先级的时段有冲突，
    // 则系统会自动注销低优先级的时段，如果App的优先级相同，则后添加的App不能注册。

    //一句话，通过输入的字符串时间段，筛选出当前时间段最符合要求的那个，然后存入一个List
    private static String getResult(ArrayList<App> apps, int queryTime) {
        //记录已经注册的App
        ArrayList<App> registeredApps=new ArrayList<>();
        for (App app : apps) {
            if (app.startTime>=app.endTime) continue;
            //
           ArrayList<Integer> idxs=new ArrayList<>();

            for (int i = 0; i < registeredApps.size(); i++) {
                App registered = registeredApps.get(i);
                if (registered.startTime>=app.endTime||app.startTime>=registered.endTime) continue;

                if (app.priority>registered.priority){
                    idxs.add(i);
                }else {
                    continue;
                }

                for (int j = idxs.size()-1; j >=0; j--) {
                    int deleteIdx= idxs.get(j);
                    registeredApps.remove(deleteIdx);
                }
                registeredApps.add(app);
            }
        }
        String ans = "NA";
        // 注册成功的App时段之间互不冲突，因此queryTime只会对应一个App
        for (App a : registeredApps) {
            if (queryTime >= a.startTime && queryTime < a.endTime) {
                ans = a.name;
                break;
            }
        }
        return ans;
    }
}
